package com.example.network;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.example.network.HttpURLConnection.UrlConnManager;
import com.example.network.JSON_parsing.JSON_Fastjson;
import com.example.network.JSON_parsing.JSON_GSON;
import com.example.network.JSON_parsing.JSON_Jackson;
import com.example.network.JSON_parsing.JSON_Native;
import com.example.network.XML_Parsing.DomHelper;
import com.example.network.XML_Parsing.PullHelper;
import com.example.network.XML_Parsing.SaxHelper;
import com.example.network.entity.Person;
import com.example.network.framework_okhttp.GetRequest;
import com.example.network.framework_okhttp.PostRequest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UrlConnManager urlConnManager = new UrlConnManager();
//        urlConnManager.useHttpUrlConnectionGetThread_get();
        urlConnManager.useHttpUrlConnectionGetThread_post();
        Button sax_parsing = findViewById(R.id.sax_parsing);
        sax_parsing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Person> peoples = null;
                SaxHelper saxHelper = new SaxHelper(MainActivity.this);
                try {
                    peoples = saxHelper.readxmlForSAX("person1.xml");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (Person people : peoples) {
                    Log.i("people", people.toString());
                }
            }
        });

        Button dom_parsing = findViewById(R.id.dom_parsing);
        dom_parsing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Person> peoples = DomHelper.queryXML(MainActivity.this, "person1.xml");
                for (Person people : peoples) {
                    Log.i("people", people.toString());
                }
            }
        });

        Button pull_parsing = findViewById(R.id.pull_parsing);
        pull_parsing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Person> peoples = null;
                try {
                    peoples = PullHelper.getPersons(MainActivity.this, "person1.xml");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (Person people : peoples) {
                    Log.i("people", people.toString());
                }
            }
        });

        Button pull_create = findViewById(R.id.pull_create);
        pull_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Person> peoples = null;
                try {
                    peoples = PullHelper.getPersons(MainActivity.this, "person1.xml");
                    FileOutputStream fileOutputStream = openFileOutput("person2.xml", Context.MODE_APPEND);
                    PullHelper.save(peoples, fileOutputStream);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (Person people : peoples) {
                    Log.i("people", people.toString());
                }
            }
        });

        Button JSON_parsing = findViewById(R.id.JSON_parsing);
        JSON_parsing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person = new Person(0, "0", 0);
                ArrayList<Person> persons = new ArrayList<>();
                HashMap<String, Person> personsM = new HashMap<>();
                for (int i = 0; i < 10; i++) {
                    persons.add(new Person(i + 1, i + 1 + "", i + 1));
                }
                for (int i = 0; i < 10; i++) {
                    personsM.put(i+"", new Person(i + 1, i + 1 + "", i + 1));
                }
                String personS = JSON.toJSONString(person);
                String personsS = JSON.toJSONString(persons);
                String personsMS = JSON.toJSONString(personsM);

                Log.i("JSON——person", personS);
                Log.i("JSON——person_list", personsS);
                Log.i("JSON——person_map", personsMS);


                JSON_Native json_native = new JSON_Native();
                Person person1 = json_native.ObjectanalysisTest(personS);
                Log.i("JSON_Native", person1.toString());
                ArrayList<Person> persons1 = json_native.ArrayanalysisTest(personsS);
                Log.i("JSON_Native", persons1.toString());

//                ArrayList<Person> persons1 = json_native.ArrayanalysisTest(personsS);
//                Log.i("JSON_Native",persons1.toString());

                JSON_Fastjson json_fastjson = new JSON_Fastjson();
                Person person2 = json_fastjson.AnalyticalObject(personS);
                Log.i("JSON_Fastjson", person2.toString());
                ArrayList<Person> persons2 = (ArrayList) JSON_Fastjson.AnalyticalArrayObject(personsS);
                Log.i("JSON_Fastjson", persons2.toString());
                Map<String, Person> personsM2 = JSON_Fastjson.AnalyticalMapObject(personsMS);
                Log.i("JSON_Fastjson", personsM2.toString());
                for (String s : personsM2.keySet()) {
                    System.out.println(personsM2.get(s));
                }

                Person person3 = JSON_GSON.AnalyticalObject(personS);
                ArrayList<Person> persons3 = JSON_GSON.AnalyticalArrayObject(personsS);
                Map<String, Person> personM3 = JSON_GSON.AnalyticalMapObject(personsMS);

                Log.i("JSON_GSON_p", person3.toString());
                Log.i("JSON_GSON_L", persons3.toString());
                Log.i("JSON_GSON_M", personM3.toString());
                for (String s : personM3.keySet()) {
                    System.out.println(personM3.get(s));
                }

                Person person4 = null;
                List<Person> persons4=null;
                Map<String, Person> personM4 = null;
                try {
                    person4 = JSON_Jackson.json2pojo(personS, Person.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    persons4 = JSON_Jackson.json2list(personsS, Person.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    personM4 = JSON_Jackson.json2map(personsMS, Person.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                assert person4 != null;
                Log.i("JSON_GSON_p", person4.toString());
                Log.i("JSON_GSON_L", persons4.toString());
                Log.i("JSON_GSON_M", personM4.toString());
                for (String s : personM3.keySet()) {
                    System.out.println(personM4.get(s));
                }
            }
        });

        urlConnManager.useHttpUrlConnectionGetThread_post();
        Button getRequest = findViewById(R.id.getRequest);
        getRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, GetRequest.class);
                startActivity(intent);
            }
        });

        urlConnManager.useHttpUrlConnectionGetThread_post();
        Button postRequest = findViewById(R.id.postRequest);
        postRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PostRequest.class);
                startActivity(intent);
            }
        });
    }
}